package cn.stylefeng.guns.modular.cb.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbCustomerCompany;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerCompanyParam;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerCompanyResult;
import cn.stylefeng.guns.modular.cb.service.CbCustomerCompanyService;
import cn.stylefeng.guns.modular.cb.service.WxMsgService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 控制器
 *
 * @author wj
 * @Date 2021-01-07 22:44:39
 */
@Controller
@RequestMapping("/api/customer_company")
public class CbCustomerCompanyController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(CbCustomerCompanyController.class);

    private String PREFIX = "/cb/cbCustomerCompany";

    @Autowired
    private CbCustomerCompanyService cbCustomerCompanyService;

    @Autowired
    private UserService userService;

    @Autowired
    private WxMsgService wxMsgService;

    /**
     * 跳转到主页面
     *
     * @author wj
     * @Date 2021-01-16
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/cbCustomerCompany.html";
    }

    /**
     * 新增页面
     *
     * @author wj
     * @Date 2021-01-16
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/cbCustomerCompany_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wj
     * @Date 2021-01-16
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/cbCustomerCompany_edit.html";
    }

    /**
     * 审核页面
     *
     * @author wj
     * @Date 2021-01-16
     */
    @RequestMapping("/review")
    public String review() {
        return PREFIX + "/cbCustomerCompany_review.html";
    }

    /**
     * 审核页面
     *
     * @author wj
     * @Date 2021-01-16
     */
    @RequestMapping("/detail_p")
    public String detail() {
        return PREFIX + "/cbCustomerCompany_detail.html";
    }

    /**
     * 新增接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(CbCustomerCompanyParam cbCustomerCompanyParam) {
//        LoginUser user = LoginContextHolder.getContext().getUser();

        //添加企业用户角色
//        this.userService.setRoles(user.getId(), String.valueOf(6));

//        cbCustomerCompanyParam.setPhone(user.getAccount());
//        cbCustomerCompanyParam.setStatus(1);
        this.cbCustomerCompanyService.add(cbCustomerCompanyParam);

//        CbCustomerCompanyParam param = new CbCustomerCompanyParam();
//        param.setCreateUser(user.getId());
//        CbCustomerCompanyResult cbCustomerCompanyResult =  this.cbCustomerCompanyService.getCompany(param);
//        return ResponseData.success(cbCustomerCompanyResult);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(CbCustomerCompanyParam cbCustomerCompanyParam) {
        cbCustomerCompanyParam.setStatus(1);//审核中
        this.cbCustomerCompanyService.update(cbCustomerCompanyParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(CbCustomerCompanyParam cbCustomerCompanyParam) {
        this.cbCustomerCompanyService.delete(cbCustomerCompanyParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(CbCustomerCompanyParam cbCustomerCompanyParam) {
        CbCustomerCompany detail = this.cbCustomerCompanyService.getById(cbCustomerCompanyParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayuiPageInfo list(CbCustomerCompanyParam cbCustomerCompanyParam) {
        return this.cbCustomerCompanyService.findPageBySpec(cbCustomerCompanyParam);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/query_company")
    @ResponseBody
    public ResponseData queryCompany(CbCustomerCompanyParam cbCustomerCompanyParam) {
        List<CbCustomerCompanyResult> list = cbCustomerCompanyService.findListBySpec(cbCustomerCompanyParam);
        return ResponseData.success(list);
    }

    /**
     * 审核
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/checkItem")
    @ResponseBody
    public ResponseData checkItem(CbCustomerCompanyParam cbCustomerCompanyParam) {
        try{
            this.cbCustomerCompanyService.update(cbCustomerCompanyParam);

            User user = userService.getById(cbCustomerCompanyParam.getCreateUser());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("thing1", user.getAccount());
            jsonObject.put("thing5", "您的申请已审批，请查看");
            wxMsgService.sendDYTemplateMessage(user.getOpenId(),"pages/login/index/index", "IJNQwvUi_E1FdvTRdXgY6WdJx6YtFuFAhO7yrTaO_5Y", jsonObject);
        }catch (Exception e) {
            logger.error("company check error: ", e);
        }
        return ResponseData.success();
    }

    /**
     * 获取企业信息
     * @param cbCustomerCompanyParam
     * @return
     */
    @RequestMapping("/get_company")
    @ResponseBody
    public ResponseData getCompany(CbCustomerCompanyParam cbCustomerCompanyParam){
        CbCustomerCompanyResult cbCustomerCompanyResult = new CbCustomerCompanyResult();
        try{
            cbCustomerCompanyResult = this.cbCustomerCompanyService.getCompany(cbCustomerCompanyParam);
        }catch (Exception e) {
            logger.error("get_company error: ", e);
        }
        if (cbCustomerCompanyResult==null || StringUtils.isEmpty(cbCustomerCompanyResult.getId())){
            return ResponseData.error(500, "企业获取失败");
        }
        return ResponseData.success(cbCustomerCompanyResult);
    }

    /**
     * 通过登录用户获取企业信息
     * @return
     */
    @RequestMapping("/get_company_user")
    @ResponseBody
    public ResponseData getCompanyByUser(){
        LoginUser user = LoginContextHolder.getContext().getUser();
        CbCustomerCompanyParam param = new CbCustomerCompanyParam();
        param.setCreateUser(user.getId());
        CbCustomerCompanyResult cbCustomerCompanyResult =  this.cbCustomerCompanyService.getCompany(param);
        cbCustomerCompanyResult.setLoginUser(user);
        return ResponseData.success(cbCustomerCompanyResult);
    }
}